package portal.manager.data;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Sorrentino
 *
 */
public class Page extends Template {
	
	@SuppressWarnings("serial")
	public static class Variable extends Template.Variable {
	
		private String value;

		public Variable() {
			
		}
	
		public Variable( Page page, Template.Variable var) {
			super( page, var.getName(), var.isBlock(), var.isCodeModule() );
		}

		public final String getValue() {
			return value;
		}

		public final void setValue(String panel) {
			this.value = panel;
		}
		
	}
	
	private String title;
	
	public Page() {
		
	}

	@SuppressWarnings("unchecked")
	public Page( String name, String title, Template tpl ) {
	
		assert null!=name;
		assert null!=tpl;
		
		if( null==name ) throw new IllegalArgumentException( "name is null!");
		if( null==tpl ) throw new IllegalArgumentException( "template is null!");
		
		this.setTitle(title);
		
		List<Template.Variable> vars = (List<Template.Variable>) tpl.getVariables();
		
		assert null!=vars && vars.isEmpty()==false;
		if( null==vars || vars.isEmpty() ) throw new IllegalArgumentException( "variables are not present!");
		
		setName( name );
		setSource(tpl.getSource());
		
		List<Template.Variable> pageVars = new ArrayList<Template.Variable>( vars.size() );
		
		for( Template.Variable v : vars ) {
			
			Template.Variable pv = new Page.Variable( this, v );
			
			pageVars.add( pv );
		}
		
		this.setVariables(pageVars);
				
	}
	
	@SuppressWarnings("unchecked")
	public Page( String name, Template tpl ) {
		this( name, name, tpl);
	}
	
	/**
	 * 
	 * @param variableName
	 * @param panel
	 */
	public void setPanel( String variableName, String panel ) {
		
		for( Template.Variable v : getVariables() ) {
			
			if( variableName.equalsIgnoreCase(v.getName()) ) {
				
				Variable vv = (Variable)v;
				
				vv.setValue(panel);
				
				break;
			}
		}
	}

	public final String getTitle() {
		return title;
	}

	public final void setTitle(String title) {
		this.title = title;
	} 
}
